package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import javax.xml.datatype.DatatypeFactory;  
import javax.xml.datatype.XMLGregorianCalendar;  
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import com.lb.jwitter.service.ConditionReport; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.util.Message; 
import java.util.Collections; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import java.util.List; 
import java.util.Map; 
import com.lb.jwitter.util.ApplicationContextProvider; 
 import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.lb.jwitter.service.TblCTDTConfig;
import com.lb.jwitter.service.TblCTDTConfigService;

import com.lb.jwitter.service.TblCTDTHeDaoTao;
import com.lb.jwitter.service.TblCTDTHeDaoTaoService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblCTDTConfigAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private String idList; 
    public String getIdList() { return idList; } 
    public void setIdList(String idList) { this.idList = idList; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public Long getId() {
       return id;
 }

    public void setId(Long id) {
       this.id = id;
 }

    private int heDaoTaoId; 
    public void setHeDaoTaoId(int heDaoTaoId) {
       this.heDaoTaoId = heDaoTaoId;
 }

    public int getHeDaoTaoId() {
       return heDaoTaoId;
 }

    private int khoaHoc; 
    public void setKhoaHoc(int khoaHoc) {
       this.khoaHoc = khoaHoc;
 }

    public int getKhoaHoc() {
       return khoaHoc;
 }

    private int soTCMin; 
    public void setSoTCMin(int soTCMin) {
       this.soTCMin = soTCMin;
 }

    public int getSoTCMin() {
       return soTCMin;
 }

    private int soTCMax; 
    public void setSoTCMax(int soTCMax) {
       this.soTCMax = soTCMax;
 }

    public int getSoTCMax() {
       return soTCMax;
 }

    private int soHKChinh; 
    public void setSoHKChinh(int soHKChinh) {
       this.soHKChinh = soHKChinh;
 }

    public int getSoHKChinh() {
       return soHKChinh;
 }

    private int soHKMax; 
    public void setSoHKMax(int soHKMax) {
       this.soHKMax = soHKMax;
 }

    public int getSoHKMax() {
       return soHKMax;
 }

    private int soTCHKMax; 
    public void setSoTCHKMax(int soTCHKMax) {
       this.soTCHKMax = soTCHKMax;
 }

    public int getSoTCHKMax() {
       return soTCHKMax;
 }

    private int soTCDieuChinh; 
    public void setSoTCDieuChinh(int soTCDieuChinh) {
       this.soTCDieuChinh = soTCDieuChinh;
 }

    public int getSoTCDieuChinh() {
       return soTCDieuChinh;
 }

    private TblCTDTConfig tblCTDTConfig; 
    public TblCTDTConfig getTblCTDTConfig () {
        return this.tblCTDTConfig ;
 }

    private Collection<TblCTDTConfig> tblCTDTConfigs; 
    public Collection<TblCTDTConfig> getTblCTDTConfigs() {
        return this.tblCTDTConfigs; 
      } 

    private TblCTDTConfigService tblCTDTConfigService; 

    private Collection<TblCTDTHeDaoTao> tblCTDTHeDaoTaos; 
    public Collection<TblCTDTHeDaoTao> getTblCTDTHeDaoTaos() {
         return this.tblCTDTHeDaoTaos; 
    }

    private TblCTDTHeDaoTaoService tblCTDTHeDaoTaoService; 

	private int preUpdate(Message msg, TblCTDTConfig obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblCTDTConfig obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String update() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_Config", "sua")){ 
        return "noprivilege"; 
    } 
           tblCTDTConfigService = (TblCTDTConfigService)context.getBean("tbl_CTDT_ConfigClient"); 
           tblCTDTHeDaoTaoService = (TblCTDTHeDaoTaoService)context.getBean("tbl_CTDT_HeDaoTaoClient"); 
           msg = new Message(); 
        tblCTDTConfig = tblCTDTConfigService.gettblCTDTConfig(new Long(id)); 
        tblCTDTConfig.setHeDaoTaoId(tblCTDTHeDaoTaoService.gettblCTDTHeDaoTao(new Long(heDaoTaoId))); 
        tblCTDTConfig.setKhoaHoc(khoaHoc); 
        tblCTDTConfig.setSoTCMin(soTCMin); 
        tblCTDTConfig.setSoTCMax(soTCMax); 
        tblCTDTConfig.setSoHKChinh(soHKChinh); 
        tblCTDTConfig.setSoHKMax(soHKMax); 
        tblCTDTConfig.setSoTCHKMax(soTCHKMax); 
        tblCTDTConfig.setSoTCDieuChinh(soTCDieuChinh); 
        preUpdate(msg,tblCTDTConfig); 
       if (msg.isPreMessage_b()) 
       { 
           tblCTDTConfigService.updatetblCTDTConfig(tblCTDTConfig); 
           postUpdate(msg, tblCTDTConfig); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblCTDTConfigs =       tblCTDTConfigService.findAlltblCTDTConfig(pageSize, page); 
      pages = tblCTDTConfigService.counttblCTDTConfigByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&heDaoTaoId="; 
      dataString += "&khoaHoc="; 
      dataString += "&soTCMin="; 
      dataString += "&soTCMax="; 
      dataString += "&soHKChinh="; 
      dataString += "&soHKMax="; 
      dataString += "&soTCHKMax="; 
      dataString += "&soTCDieuChinh="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_CTDT_Config Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_Config", "xem")){ 
        return "noprivilege"; 
    } 
           tblCTDTConfigService = (TblCTDTConfigService)context.getBean("tbl_CTDT_ConfigClient"); 
       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        tblCTDTConfigs =        tblCTDTConfigService.findAlltblCTDTConfig(pageSize, page); 
        pages = tblCTDTConfigService.counttblCTDTConfigByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&heDaoTaoId="; 
      dataString += "&khoaHoc="; 
      dataString += "&soTCMin="; 
      dataString += "&soTCMax="; 
      dataString += "&soHKChinh="; 
      dataString += "&soHKMax="; 
      dataString += "&soTCHKMax="; 
      dataString += "&soTCDieuChinh="; 
           tblCTDTHeDaoTaoService = (TblCTDTHeDaoTaoService)context.getBean("tbl_CTDT_HeDaoTaoClient"); 
      tblCTDTHeDaoTaos = tblCTDTHeDaoTaoService.findAlltblCTDTHeDaoTao(0, 0);
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_CTDT_Config Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
  public String delete() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_Config", "xoa")){ 
        return "noprivilege"; 
    }           tblCTDTConfigService = (TblCTDTConfigService)context.getBean("tbl_CTDT_ConfigClient"); 
    msg = new Message(); 

    List<String> ids = new ArrayList<String>(); 
    Collections.addAll(ids, idList.split(",")); 
    for(String id : ids){ 
        tblCTDTConfigService.deleteListtblCTDTConfig(ids); 
    } 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblCTDTConfigs =       tblCTDTConfigService.findAlltblCTDTConfig(pageSize, page); 
      pages = tblCTDTConfigService.counttblCTDTConfigByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&heDaoTaoId="; 
      dataString += "&khoaHoc="; 
      dataString += "&soTCMin="; 
      dataString += "&soTCMax="; 
      dataString += "&soHKChinh="; 
      dataString += "&soHKMax="; 
      dataString += "&soTCHKMax="; 
      dataString += "&soTCDieuChinh="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Delete tbl_CTDT_Config Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 	private int preInsert(Message msg, TblCTDTConfig obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblCTDTConfig obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String insert() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_Config", "them")){ 
        return "noprivilege"; 
    }           tblCTDTConfigService = (TblCTDTConfigService)context.getBean("tbl_CTDT_ConfigClient"); 
           tblCTDTHeDaoTaoService = (TblCTDTHeDaoTaoService)context.getBean("tbl_CTDT_HeDaoTaoClient"); 
        msg = new Message(); 
        tblCTDTConfig = new TblCTDTConfig(); 
        tblCTDTConfig.setHeDaoTaoId(tblCTDTHeDaoTaoService.gettblCTDTHeDaoTao(new Long(heDaoTaoId))); 
        tblCTDTConfig.setKhoaHoc(khoaHoc); 
        tblCTDTConfig.setSoTCMin(soTCMin); 
        tblCTDTConfig.setSoTCMax(soTCMax); 
        tblCTDTConfig.setSoHKChinh(soHKChinh); 
        tblCTDTConfig.setSoHKMax(soHKMax); 
        tblCTDTConfig.setSoTCHKMax(soTCHKMax); 
        tblCTDTConfig.setSoTCDieuChinh(soTCDieuChinh); 
        preInsert(msg,tblCTDTConfig); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblCTDTConfigService.createtblCTDTConfig(tblCTDTConfig); 
           postInsert(msg, tblCTDTConfig); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblCTDTConfig.setId(id); 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      tblCTDTConfigs =       tblCTDTConfigService.findAlltblCTDTConfig(pageSize, page); 
      pages = tblCTDTConfigService.counttblCTDTConfigByCondition(new ArrayList<ConditionReport>())/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&heDaoTaoId="; 
      dataString += "&khoaHoc="; 
      dataString += "&soTCMin="; 
      dataString += "&soTCMax="; 
      dataString += "&soHKChinh="; 
      dataString += "&soHKMax="; 
      dataString += "&soTCHKMax="; 
      dataString += "&soTCDieuChinh="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_CTDT_Config Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 	private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
 public String search() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_Config", "xem")){ 
        return "noprivilege"; 
    } 
           tblCTDTConfigService = (TblCTDTConfigService)context.getBean("tbl_CTDT_ConfigClient"); 
        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
    if(id!= null && !id.equals(0)){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("id");
        con.setValue1(new Long(id));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(heDaoTaoId != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("heDaoTaoId.id");
        con.setValue1(new Long(heDaoTaoId));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(khoaHoc != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("khoaHoc");
        con.setValue1(khoaHoc);
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(soTCMin != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("soTCMin");
        con.setValue1(soTCMin);
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(soTCMax != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("soTCMax");
        con.setValue1(soTCMax);
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(soHKChinh != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("soHKChinh");
        con.setValue1(soHKChinh);
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(soHKMax != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("soHKMax");
        con.setValue1(soHKMax);
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(soTCHKMax != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("soTCHKMax");
        con.setValue1(soTCHKMax);
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(soTCDieuChinh != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("soTCDieuChinh");
        con.setValue1(soTCDieuChinh);
        con.setOperator("eq");
        cons.add(con);	
    } 
        preSearch(msg, cons); 
       if (msg.isPreMessage_b()) 
       { 
           if(pageSize == 0){ 
               pageSize = 20; 
           } 
            if(page == 0){ 
               page =1; 
           } 
           tblCTDTConfigs = tblCTDTConfigService.findtblCTDTConfigByCondition(cons,pageSize,page); 
           pages = tblCTDTConfigService.counttblCTDTConfigByCondition(cons)/pageSize + 1; 
           postSearch(msg, cons); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
      dataString = ""; 
      dataString += "&id=" + id; 
      dataString += "&heDaoTaoId=" + heDaoTaoId; 
      dataString += "&khoaHoc=" + khoaHoc; 
      dataString += "&soTCMin=" + soTCMin; 
      dataString += "&soTCMax=" + soTCMax; 
      dataString += "&soHKChinh=" + soHKChinh; 
      dataString += "&soHKMax=" + soHKMax; 
      dataString += "&soTCHKMax=" + soTCHKMax; 
      dataString += "&soTCDieuChinh=" + soTCDieuChinh; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Seach tbl_CTDT_Config Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
  public String editForm() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_Config", "sua")){ 
        return "noprivilege"; 
    } 
           tblCTDTConfigService = (TblCTDTConfigService)context.getBean("tbl_CTDT_ConfigClient"); 
        tblCTDTConfig = tblCTDTConfigService.gettblCTDTConfig(new Long(id)); 
           tblCTDTHeDaoTaoService = (TblCTDTHeDaoTaoService)context.getBean("tbl_CTDT_HeDaoTaoClient"); 
        tblCTDTHeDaoTaos = tblCTDTHeDaoTaoService.findAlltblCTDTHeDaoTao(0, 0);

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi EditForm tbl_CTDT_Config Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 } 
 
